import crypto from 'crypto-es';
const SECURE_KEY = '_crypto_secure_key';

export function aesMinEncrypt(word) {
  const _word = crypto.enc.Utf8.parse(word),
    _key = crypto.enc.Utf8.parse('ihaierForTodoKey'),
    _iv = crypto.enc.Utf8.parse('ihaierForTodo_Iv');
  const encrypted = crypto.AES.encrypt(_word, _key, {
    iv: _iv,
    mode: crypto.mode.CBC,
    padding: crypto.pad.Pkcs7,
  });
  return encrypted.toString();
}

export function encrypt(data, secureKey) {
  if (secureKey === undefined) {
    secureKey = SECURE_KEY;
  }
  return crypto.AES.encrypt(JSON.stringify(data), secureKey).toString();
}
export function decrypt(data, secureKey) {
  if (secureKey === undefined) {
    secureKey = SECURE_KEY;
  }
  data = crypto.AES.decrypt(data, secureKey);
  try {
    return JSON.parse(data.toString(crypto.enc.Utf8));
  } catch (e) {
    return data;
  }
}
